/*
 * TCombo.java
 *
 * Created on July 23, 2005, 1:50 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package ge.ktapestry.propertyselections;

import org.apache.tapestry.form.IPropertySelectionModel;

import java.util.List;

public class ComboList implements IPropertySelectionModel, java.io.Serializable
{

    /**
     * Creates a new instance of TCombo
     */
    private List<IComboItem> options;

    public ComboList()
    {
    }

    public String getLabel(int param)
    {
        return (options.get(param)).getDisplayName();
    }

    public Object getOption(int param)
    {
        return options.get(param);
    }

    public int getOptionCount()
    {
        return options.size();
    }

    public String getValue(int param)
    {
        return Integer.toString(param);
    }

    public Object translateValue(String str)
    {
        try
        {
            int i = Integer.parseInt(str);
            return options.get(i);
        }
        catch (NumberFormatException nf)
        {
            return null; //may get here when there are no options
        }
    }

    public List getOptions()
    {
        return options;
    }

    public void setOptions(List options)
    {
        this.options = options;
    }

    public boolean containsPK(String uniquePK)
    {
        for (int i = 0; i < options.size(); i++)
        {
            if (options.get(i).getUniqueID().equalsIgnoreCase(uniquePK) && options.get(i).getDisplayName() != null)
            {
                return true;
            }
        }
        return false;
    }

    public String getLabelFromPk(String uniquePK)
    {
        for (int i = 0; i < options.size(); i++)
        {
            if (options.get(i).getUniqueID().equalsIgnoreCase(uniquePK) && options.get(i).getDisplayName() != null)
            {
                return options.get(i).getDisplayName();
            }
        }
        return uniquePK;
    }

    public Object getOptionFromPk(String uniquePK)
    {
        for (int i = 0; i < options.size(); i++)
        {
            if (options.get(i).getUniqueID().equalsIgnoreCase(uniquePK) && options.get(i).getDisplayName() != null)
            {
                return options.get(i);
            }
        }
        return null;
    }

    public Object getOptionFromPk(Integer uniquePK)
    {
        return getOptionFromPk(String.valueOf(uniquePK));
    }

    public boolean isDisabled(int arg0)
    {
        return false;
    }
}
